package dk.hewison.client.slideshow;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.Widget;
import dk.hewison.client.Log;
import dk.hewison.client.component.MyImage;
import dk.hewison.client.domain.Photo;
import dk.hewison.client.domain.Slideshow;
import dk.hewison.client.panels.CompositeView;

import java.util.List;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-09 12:39:39 +0000 (Mon, 09 Feb 2009) $:
 * @version $Revision: 363 $:
 */
public class SlideshowView extends CompositeView<Slideshow> {

    SlideshowControlView slideshowControlView;
    WidgetEffectPanel mainPhotoView;

    FocusPanel focusPanel = new FocusPanel();

    List<Photo> photos;
    int pos = -1;
    private Slideshow slideshow;
    private Timer timer;

    private AbsolutePanel absolutePanel;


    public void initView() {


        absolutePanel = new AbsolutePanel();

        initWidget(absolutePanel);
        setSize("100%", "100%");
        setStyleName("slideshowview");


        slideshowControlView = new SlideshowControlView(getController().getActions());

        sinkEvents(Event.MOUSEEVENTS);
    }

    @Override
    public void onBrowserEvent(Event event) {
        //super.onBrowserEvent(event);

        switch (DOM.eventGetType(event)) {
            case Event.ONMOUSEMOVE: {
                slideshowControlView.show();
                break;
            }
        }
    }

    public void setModel(Slideshow slideshow) {
        this.slideshow = slideshow;
        photos = slideshow.getPhotos();
    }

    /**
     * Start
     */
    public void startSlideshow() {
        pos = -1;
        if (timer != null) {
            timer.cancel();
        }
        timer = new Timer() {
            @Override
            public void run() {
                nextPhoto();
            }
        };
        play();
    }

    public void stopSlideshow() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        pos = -1;
        absolutePanel.clear();
    }

    private void nextPhoto() {
        Log.debug("Size " + getOffsetWidth());
        if (pos++ >= photos.size() - 1) {
            pos = 0;
        }

        if (mainPhotoView == null) {
            doNextPhoto();
        } else {

            Log.debug("fadeOut");
            mainPhotoView.fadeOut(new TransitionCallback() {
                public void transtionFinished() {
                    doNextPhoto();
                }
            });

        }

    }

    private void doNextPhoto() {
        final MyImage myImage = getNextImage();

        myImage.addLoadListener(new LoadListener() {
            public void onError(Widget sender) {

            }

            public void onLoad(Widget sender) {
                double a = getOffsetWidth();
                double b = getOffsetHeight();

                double w = myImage.getWidth();
                double h = myImage.getHeight();

                //fit the image to fill the window
                //if the window is 800x600 and the image is 1000x800
                //then 1000/800 = 1.25
                //and 800/600 = 1.33
                //this means that the image can be reduced by 1.25
                double widthP = w / a;
                double heightP = h / b;

                double factor = Math.max(widthP, heightP);

                w = (w / factor) - 20;
                h = (h / factor) - 20;

                myImage.setPixelSize((int) w, (int) h);

                absolutePanel.clear();
                //Log.debug("" + myImage.getWidth());
                mainPhotoView = new WidgetEffectPanel(myImage);

                double x = (a - w) / 2;
                double y = (b - h) / 2;
                absolutePanel.add(mainPhotoView, (int) x, (int) y);
                mainPhotoView.fadeIn();
            }
        });


        absolutePanel.add(myImage, 2000, 2000);

        /*absolutePanel.setCellHorizontalAlignment(mainPhotoView, HorizontalPanel.ALIGN_CENTER);
       absolutePanel.setCellVerticalAlignment(mainPhotoView, HorizontalPanel.ALIGN_MIDDLE);*/

    }

    public MyImage getNextImage() {
        return new MyImage(photos.get(pos), false);
    }

    public void exit() {
        slideshowControlView.hide();
    }

    public void pause() {
        timer.cancel();
    }

    public void play() {
        timer.scheduleRepeating(slideshow.getTimedelay());
        timer.run();
    }
}
